package org.nanotek.tika.tests.mq.send;

import java.io.File;
import java.util.Date;

import org.nanotek.cms.domain.lucene.BaseDocument;
import org.nanotek.cms.domain.lucene.Index;
import org.nanotek.cms.domain.message.BodyMessageContainer;
import org.nanotek.cms.domain.message.MessageType;
import org.nanotek.dao.DAO;
import org.nanotek.lucene.index.search.AnalyzerType;
import org.nanotek.lucene.index.search.IndexType;
import org.nanotek.mq.send.MetaIndexerSenderService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sun.syndication.feed.atom.Link;

public class IndexCloseSenderTest {

	private static ClassPathXmlApplicationContext ctx;

	public IndexCloseSenderTest() {
	}

	/**
	 * @param args
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException {
		ctx = new ClassPathXmlApplicationContext(new String[] {"activemq-config/base-activemq-sender-context.xml","base-index-config/index-service-context.xml"});
		MetaIndexerSenderService sender =  (MetaIndexerSenderService) ctx.getBean("metaIndexerSenderService");
		Index index = new Index();//constructIndexRequest();
		BodyMessageContainer msgContainer = new BodyMessageContainer();
		msgContainer.setId(new Date().getTime());
		msgContainer.setMessageType(MessageType.IndexClose);
		msgContainer.setBodyMessage(index);
		sender.sendMessage(msgContainer);
		System.exit(0);
	}

	public static Index constructIndexRequest()
	{ 
		File idxFile = (File) ctx.getBean("indexFilePath");
		Index index = new Index(); 
		Link location = new Link(); 
		location.setTitle("Base Index Writer Config Test"); 
		location.setHref(idxFile.getPath());
		index.setLocation(location); 
		index.setIndexAlias("A simple base index alias"); 
		index.setIndexType(IndexType.NMAPDIRECTORY);
		index.setAnalyzerType(AnalyzerType.STANDARD);
		DAO<Link> linkDAO = (DAO<Link>)ctx.getBean("linkDAO");
		DAO<Index> indexDAO = (DAO<Index>) ctx.getBean("indexDAO"); 
		linkDAO.update(location);
		indexDAO.update(index);

		return index; 
	}

}
